﻿using System;
using System.IO;
using System.Windows.Forms;

namespace HW_e3
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }

        private void buttonSelectFile1_Click(object sender, EventArgs e)
        {
            OpenFileDialog openFileDialog = new OpenFileDialog();
            if (openFileDialog.ShowDialog() == DialogResult.OK)
            {
                textBoxFile1.Text = openFileDialog.FileName;
            }
        }

        private void buttonSelectFile2_Click(object sender, EventArgs e)
        {
            OpenFileDialog openFileDialog = new OpenFileDialog();
            if (openFileDialog.ShowDialog() == DialogResult.OK)
            {
                textBoxFile2.Text = openFileDialog.FileName;
            }
        }

        private void buttonMergeFiles_Click(object sender, EventArgs e)
        {
            string file1Path = textBoxFile1.Text;
            string file2Path = textBoxFile2.Text;

            if (string.IsNullOrEmpty(file1Path) || string.IsNullOrEmpty(file2Path))
            {
                MessageBox.Show("请选择两个文件。");
                return;
            }
            try
            {
                string dataDirectory = Path.Combine(AppDomain.CurrentDomain.BaseDirectory, "Data");
                if (!Directory.Exists(dataDirectory))
                {
                    Directory.CreateDirectory(dataDirectory);
                }

                string mergedFilePath = Path.Combine(dataDirectory, "NewFile.txt");

                using (StreamWriter writer = new StreamWriter(mergedFilePath))
                {
                    writer.WriteLine(File.ReadAllText(file1Path));
                    writer.WriteLine(File.ReadAllText(file2Path));
                }

                MessageBox.Show($"文件已合并并保存到: {mergedFilePath}");
            }
            catch (Exception ex)
            {
                MessageBox.Show($"合并文件时出错: {ex.Message}");
            }
        }
    }
}